package com.lk.ii.controller;

import java.util.List;

import com.lk.framework.util.ShiroUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.lk.common.annotation.Log;
import com.lk.common.enums.BusinessType;
import com.lk.ii.domain.IiCase;
import com.lk.ii.domain.IiSolution;
import com.lk.ii.service.IIiCaseService;
import com.lk.ii.service.IIiSolutionService;
import com.lk.common.core.controller.BaseController;
import com.lk.common.core.domain.AjaxResult;
import com.lk.common.utils.poi.ExcelUtil;
import com.lk.common.core.page.TableDataInfo;

/**
 * 案例Controller
 * 
 * @author lk
 * @date 2021-02-22
 */
@Controller
@RequestMapping("/ii/case")
public class IiCaseController extends BaseController
{
    private String prefix = "ii/case";

    @Autowired
    private IIiCaseService iiCaseService;

    @Autowired
    private IIiSolutionService iiSolutionService;

    @RequiresPermissions("ii:case:view")
    @GetMapping()
    public String Case()
    {
        return prefix + "/case";
    }

    /**
     * 查询案例列表
     */
    @RequiresPermissions("ii:case:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(IiCase iiCase)
    {
        startPage();
        List<IiCase> list = iiCaseService.selectIiCaseList(iiCase);
        return getDataTable(list);
    }

    /**
     * 导出案例列表
     */
    @RequiresPermissions("ii:case:export")
    @Log(title = "案例", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(IiCase iiCase)
    {
        List<IiCase> list = iiCaseService.selectIiCaseList(iiCase);
        ExcelUtil<IiCase> util = new ExcelUtil<IiCase>(IiCase.class);
        return util.exportExcel(list, "case");
    }

    /**
     * 新增案例
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        IiSolution iiSolution = new IiSolution();
        iiSolution.setStatus(1);
        List<IiSolution> solutions = iiSolutionService.selectIiSolutionList(iiSolution);
        mmap.put("solutions",solutions);
        return prefix + "/add";
    }

    /**
     * 新增保存案例
     */
    @RequiresPermissions("ii:case:add")
    @Log(title = "案例", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(IiCase iiCase)
    {
        iiCase.setCreateBy(ShiroUtils.getLoginName());
        return toAjax(iiCaseService.insertIiCase(iiCase));
    }

    /**
     * 修改案例
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        IiSolution iiSolution = new IiSolution();
        iiSolution.setStatus(1);
        List<IiSolution> solutions = iiSolutionService.selectIiSolutionList(iiSolution);
        mmap.put("solutions",solutions);
        IiCase iiCase = iiCaseService.selectIiCaseById(id);
        mmap.put("iiCase", iiCase);
        return prefix + "/edit";
    }

    /**
     * 修改保存案例
     */
    @RequiresPermissions("ii:case:edit")
    @Log(title = "案例", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(IiCase iiCase)
    {
        iiCase.setUpdateBy(ShiroUtils.getLoginName());
        return toAjax(iiCaseService.updateIiCase(iiCase));
    }

    /**
     * 删除案例
     */
    @RequiresPermissions("ii:case:remove")
    @Log(title = "案例", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(iiCaseService.deleteIiCaseByIds(ids));
    }
}
